package com.troyhigh.njrotc.admintrackerold;

import java.util.Scanner;
import java.util.ArrayList;
import java.sql.Date;

public class ATCommandLine
{
    private static Scanner reader;
    private static AdminTracker admintracker;
    private static double version = 1.5;
    private static String startupMessage = "This is an nonworking version but because it fixes dates, it is going to be the working version.";
    
    public static void main(String[] args)
    {
        System.out.println("Welcome to AdminTracker, Command Line Edition!");
        System.out.println("Programmed by BRO Quan Nguyen, c/o 2010!");
        System.out.println("Version " + version);
        System.out.println("Startup message: " + startupMessage);
        admintracker = new AdminTracker();
        reader = new Scanner(System.in);
        do { System.out.print("\nAdminTracker> "); } while(getInstruction());
        admintracker.close();
        reader.close();
        System.out.println("Goodbye!");
        
    }
    
    public static boolean getInstruction()
    {
        String notprepared = reader.nextLine();
        String instruction = notprepared.trim().toLowerCase();
        if(instruction.equals("quit") || instruction.equals("exit") || instruction.equals("stop"))
        {
            return false;
        }
        else
        {
            if(instruction.equals("help"))
            {
                System.out.println("Available commands: restart, create event," +
                        " remove event, get events, input, get event, get students,"+
                        " lookup, get student, create student");
                System.out.println("See Battalion Roster Officer for more assistance.");
                return true;
            }
            else if(instruction.equals("about"))
            {
                String about = "About AdminTracker\n------------------\nProgrammed for use by the Battalion Roster Officer of Unit Troy NJROTC."+
        " This applet was programmed by Quan Nguyen, Troy High School class of 2010. The AdminTracker consists of over 1200 lines"+
        " of code (*cough* reference soup *cough*) written in the Java programming language, as of version 1.06, or revision 15 in"+
        " the Subversion repository. The source code is available for free at this project's Google Code page at <http://code.google."+
        "com/p/admintracker>. This program would not have been possible were it not for the support of:\n\nChristian Reyes, Troy"+
        " High School class of 2009\nAaron Tran, Troy High School class of 2010\nCommander Stubblefield\nChief King\nFirst Sergeant"+
        " Lyon\n\nMr. Coglianese\nMr. Reid\nMr. Allen\nMr. Wittry\n\nAdminTracker 2008, version " + version + ".";
                System.out.println(about);
                return true;
            }
            else if(instruction.equals("restart"))
            {
                return restart();
            }           
            else if(instruction.equals("create event"))
            {
                return createEvent();
            }
            else if(instruction.equals("remove event"))
            {
                return removeEvent();
            }
            else if(instruction.equals("get events"))
            {
                return getEvents();
            }
            else if(instruction.equals("input"))
            {
                return input();
            }
            else if(instruction.equals("get event"))
            {
                return getEvent();
            }
            else if(instruction.equals("get students"))
            {
                return getStudents();
            }
            else if(instruction.equals("lookup"))
            {
                return lookup();
            }
            else if(instruction.equals("get student"))
            {
                return getStudent();
            }
            else if(instruction.equals("create student"))
            {
                return createStudent();
            }
            else if(instruction.equals("output awards"))
            {
                return outputAwards();
            }
            System.out.println("Command not found.");
        }
        return true;
    }
    
    private static boolean restart()
    {
        System.out.println("Are you sure (Y/N)?");
        boolean go = reader.nextLine().toUpperCase().equals("Y");
        if(!go) return true;
        System.out.println("Restarting...");
        admintracker.close();
        System.out.println("Closing old session...");
        admintracker = new AdminTracker();
        System.out.println("Restart successful.");
        return true;
    }
    
    private static boolean getEvents()
    {
        ArrayList<Event> events = admintracker.getEvents();
        System.out.println("BEGIN EVENT LIST");
        for(Event e : events)
            System.out.println("NAME: " + e.getName() + " DATE: " + e.getDate());
        System.out.println("END EVENT LIST");
        return true;
    }
    
    private static boolean createEvent()
    {
        System.out.print("(1/5) Name? ");
        String name = reader.nextLine();
        if(name.equals("\\c")) return true;
        System.out.print("(2/5) Date (yyyy-mm-dd)? ");
        String date = reader.nextLine();
        if(date.equals("\\c")) return true;
        System.out.print("(3/5) Category? ");
        String cate = reader.nextLine().toUpperCase();
        if(cate.equals("\\c")) return true;

        String dura = "1";
        if(cate.indexOf("SERVICE") != -1)
        {
            System.out.print("(4/5) Duration? ");
            dura = reader.nextLine();
            if(dura.equals("\\c")) return true;
        }

        String cou2 = (cate.indexOf("SERVICE") != -1) ? "HOURS" : "ATTENDED";
        int dur2 = Integer.parseInt(dura);

        Event e = admintracker.createEvent(name, date, cate, cou2, dur2);
        System.out.println("Event successfully created.");
        System.out.print("(5/5) Would you like to input students now (yes/no)? ");
        String choice = reader.nextLine();
        if(choice.equals("yes")) return input(e);
        return true;
    }
    
    private static boolean removeEvent()
    {
        System.out.print("(1/3) Name? ");
        String name = reader.nextLine();
        if(name.equals("\\c")) return true;
        System.out.print("(2/3) Date? ");
        String date = reader.nextLine();
        if(date.equals("\\c")) return true;

        Event e = admintracker.getEvent(name, Date.valueOf(date));
        if(e != null)
        {
            System.out.print("(3/3) Is this correct (yes/no): " + e.getName() + ", on " + e.getDate() + "? ");
            boolean go = reader.nextLine().equals("yes");
            admintracker.removeEvent(name, java.sql.Date.valueOf(date));
            if(go) System.out.println("Event removed.");
            else System.out.println("Operation canceled.");
            return true;
        }
        System.out.println("Event not found. Operation canceled.");
        return true;
    }
    
    private static boolean input()
    {
        System.out.print("(1/4) Name of Event? ");
        String name = reader.nextLine();
        if(name.equals("\\c")) return true;
        System.out.print("(2/4) Date of Event (yyyy-mm-dd)? ");
        String date = reader.nextLine();
        if(date.equals("\\c")) return true;
        Event e = admintracker.getEvent(name, Date.valueOf(date));
        return input(e);
    }
    
    private static boolean input(Event e)
    {
        
        if(e != null)
        {
            System.out.print("(3/4) Is this correct (yes/no): " + e.getName() + ", on " + e.getDate() + "? ");
            boolean go = reader.nextLine().equals("yes");
            if(!go) return true;
            boolean stopped = false;
//            System.out.println("What do you want to do?");
//            System.out.println("1. Input each cadet individually");
//            System.out.println("2. Input one time for all cadets");
//            System.out.println("3...");
                System.out.println("Type STOP to end.");
                while(!stopped)
                {
                    System.out.print("(4/4) Student (ID/Name)? ");
                    String id = reader.nextLine();
                    if(id.toUpperCase().equals("STOP")) break;
                    id = id.toUpperCase();

                    Student s = null;
                    if(id.matches("\\d{4,}"))
                    {
                        s = admintracker.getStudent("", "", Integer.parseInt(id));
                    }
                    else if(id.matches("(\\w|\\s)++"))
                    {
                        if(admintracker.getStudents(id, "", -1).size() +
                           admintracker.getStudents("", id, -1).size() == 1)
                        {
                            Student sa = admintracker.getStudent(id, "", -1);
                            Student sb = admintracker.getStudent("", id, -1);
                            s = sa;
                            if(s == null) s = sb;
                        }
                        else
                        {
                            System.out.println("Ambiguous name, entries below:");
                            System.out.println("BEGIN AMBIGUOUS LIST");
                            ArrayList<Student> a = admintracker.getStudents(id, "", -1);
                            ArrayList<Student> b = admintracker.getStudents("", id, -1);
                            for(Student sp : a)
                                System.out.println(sp);
                            for(Student sq : b)
                                System.out.println(sq);
                            System.out.println("END AMBIGUOUS LIST");
                        }
                    }      

                    if(s == null)
                    {
                        System.out.println("Student does not exist!");
                    }
                    else
                    {        
                        int stu = s.getID();
                        if(e.getCategory().toString().equals("COMMUNITY SERVICE") ||
                                e.getCategory().toString().equals("UNIT SERVICE"))
                        {
                            System.out.print("(4/4) Hours? ");
                            String va = reader.nextLine();
                            int v = Integer.parseInt(va);
                            admintracker.addCredit(e, stu, v);
                        }
                        else
                        {
                            admintracker.addCredit(e, stu, 1);
                        }
                    }
                }
        }
        return true;
    }
    
    private static boolean getEvent()
    {
        System.out.print("(1/3) Name? ");
        String name = reader.nextLine();
        if(name.equals("\\c")) return true;
        System.out.print("(2/3) Date? ");
        String date = reader.nextLine();
        if(date.equals("\\c")) return true;

        Event e = admintracker.getEvent(name, Date.valueOf(date));
        if(e != null)
        {
            System.out.println("Retrieving data for: " + e.getName());
            System.out.println("Date: " + e.getDate() + " Category:" + e.getCategory());
            String counting = e.getProperties().getProperty("counting");
            String value = e.getProperties().getProperty("countingvalue");
            System.out.println("Counting: " + counting + " Duration: " + value);
            System.out.println("BEGIN STUDENT LIST");
            for(Student s : admintracker.getEventStudents(e))
            {
                System.out.println(s +", for " + e.get(s));
            }
            System.out.println("END STUDENT LIST");
            return true;
        }
        System.out.println("Event not found. Operation canceled.");
        return true;
    }
    
    private static boolean getStudents()
    {
        ArrayList<Student> students = admintracker.getStudents();
        System.out.println("BEGIN STUDENT LIST");
        for(Student s : students)
            System.out.println(s);
        System.out.println("END STUDENT LIST");
        return true;
    }
    
    private static boolean getStudent()
    {
        System.out.print("(1/1) Student (ID/Name)? ");
        String id = reader.nextLine();
        if(id.toUpperCase().equals("\\c")) return true;
        id = id.toUpperCase();

        Student s = null;
        if(id.matches("\\d{4,}"))
        {
            s = admintracker.getStudent("", "", Integer.parseInt(id));
        }
        else if(id.matches("(\\w|\\s)++"))
        {
            if(admintracker.getStudents(id, "", -1).size() +
               admintracker.getStudents("", id, -1).size() == 1)
            {
                Student sa = admintracker.getStudent(id, "", -1);
                Student sb = admintracker.getStudent("", id, -1);
                s = sa;
                if(s == null) s = sb;
            }
            else
            {
                System.out.println("Ambiguous name, entries below:");
                System.out.println("BEGIN AMBIGUOUS LIST");
                ArrayList<Student> a = admintracker.getStudents(id, "", -1);
                ArrayList<Student> b = admintracker.getStudents("", id, -1);
                for(Student sp : a)
                    System.out.println(sp);
                for(Student sq : b)
                    System.out.println(sq);
                System.out.println("END AMBIGUOUS LIST");
            }
        }
        
        if(s != null)   
        {
            System.out.println("Name: " + s.getName() + " | ID: " + s.getID());
            System.out.println("Platoon: " + s.getGroup() + 
                    " | Grade: " + s.getProperties().getProperty("grade"));
            System.out.println("Gender: " + s.getProperties().getProperty("gender") + 
                    " | NS Year: " + s.getProperties().getProperty("nsyear"));
            System.out.println();
            System.out.println("BEGIN EVENT RECORD");
            ArrayList<Event> es = admintracker.getStudentEvents(s);
            for(Event e : es)
                System.out.println(e.toString() +", for " + e.get(s.getID()));
            System.out.println("END EVENT RECORD");
            System.out.println(s.getRibbons().stringValues());  
        }     
        else
        {
            System.out.println("Student doesn't exist!");
        }
        return true;
    }
    
    private static boolean createStudent()
    {
        System.out.print("(1/7) Platoon? ");
        String p = reader.nextLine();
        if(p.equals("\\c")) return true;
        System.out.print("(2/7) Last Name? ");
        String l = reader.nextLine();
        if(l.equals("\\c")) return true;
        System.out.print("(3/7) First Name? ");
        String f = reader.nextLine();
        if(f.equals("\\c")) return true;
        System.out.print("(4/7) ID? ");
        String i = reader.nextLine();
        if(i.equals("\\c")) return true;
        System.out.print("(5/7) Gender (M/F)? ");
        String m = reader.nextLine();
        if(m.equals("\\c")) return true;
        System.out.print("(6/7) Grade? ");
        String g = reader.nextLine();
        if(g.equals("\\c")) return true;
        System.out.print("(7/7) NS Year? ");
        String y = reader.nextLine();
        if(y.equals("\\c")) return true;
        
        int p2 = Integer.parseInt(p);
        int i2 = Integer.parseInt(i);
        int g2 = Integer.parseInt(g);
        int y2 = Integer.parseInt(y);
        
        admintracker.createStudent(p2, l, f, i2, m, g2, y2);
        System.out.println("Student successfully created.");
        return true;
    }
        
        
    
    private static boolean lookup()
    {
        System.out.print("(1/1) Query (Date, Name, ID)? ");
        String q = reader.nextLine();
        if(q.equals("\\c")) return true;
        System.out.println("BEGIN RESULTS");
        if(q.matches("\\d{4,}")) //id number
        {
            System.out.println("Query is ID number, limited to students.");
            System.out.println(admintracker.getStudents("", "", Integer.parseInt(q)));
        }
        else if(q.matches("\\d{4}-\\d{2}-\\d{2}")) //yyyy-mm-dd format
        {
            System.out.println("Query is Date, limited to events.");
            System.out.println(admintracker.getEvents(null, Date.valueOf(q)));
        }
        else if(q.matches("(\\w|\\s)++")) //any words or spaces
        {
            System.out.println("Query is String, limited to event/student names.");
            System.out.println(admintracker.getEvents(q, null));
            System.out.println(admintracker.getStudents(q, "", -1));
            System.out.println(admintracker.getStudents("", q, -1));
        }
        else
        {
            System.out.println("Invalid query.");
        }
        System.out.print("END RESULTS");
        return true;
    }
    
    private static boolean outputAwards()
    {
        System.out.print("(1/1) Category? ");
        String c = reader.nextLine();
        if(c.equals("\\c")) return true;
        
        System.out.println("BEGIN AWARDS");
        if(c.equals("ALL"))
        {
            String[] cats = {"DRILL", "ACADEMICS", "COLOR GUARD", "PT",
                             "ORIENTEERING", "MARKSMANSHIP", "UNIT SERVICE",
                             "COMMUNITY SERVICE", "PARTICIPATION", "MINI BOOT CAMP",
                             "SEA CRUISE"};
            for(String t : cats)
            {
                Category c2 = Category.parseCategory(t);
                for(String s: admintracker.getAward(c2))
                    System.out.println(s);
            }
        }
        else
        {
            Category c2 = Category.parseCategory(c);
            for(String s : admintracker.getAward(c2))
                System.out.println(s);
        }
        System.out.println("END AWARDS");
        
        return true;
    
    }
    
    private static String read()
    {
        String input = reader.nextLine();
        input = input.trim();
        return input;
    }
}